package com.eb.utils;

import java.io.Serializable;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * about redis
 * @author Administrator
 * @param <K>
 * @param <V>
 *
 */
public class RedisUtil {
	
	private static RedisTemplate<Serializable, Serializable> redisTemplate;
	
	public static RedisTemplate<Serializable, Serializable> getRedisTemplate() {
		return redisTemplate;
	}

	public static void setRedisTemplate(
			RedisTemplate<Serializable, Serializable> redisTemplate) {
		RedisUtil.redisTemplate = redisTemplate;
	}
	
	/**
	 * 包含时间
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public boolean setValue(final String key,final String value){
		boolean boo = false;
		try{
			boo = redisTemplate.execute(new RedisCallback<Boolean>() {
				public Boolean doInRedis(RedisConnection connection) {
					byte[] key1 = redisTemplate.getStringSerializer().serialize(key);
					byte[] value1 = redisTemplate.getStringSerializer().serialize(value);
					connection.set(key1, value1);	
					return true;
				}
			});
		}catch(Exception e){
			e.printStackTrace();
		}
		return boo;
	}
	
	/**
	 * 包含时间
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public boolean setValue(final String key,final String value,final long seconds){
		boolean boo = false;
		try{
			boo = redisTemplate.execute(new RedisCallback<Boolean>() {
				public Boolean doInRedis(RedisConnection connection) {
					byte[] key1 = redisTemplate.getStringSerializer().serialize(key);
					byte[] value1 = redisTemplate.getStringSerializer().serialize(value);
					connection.setEx(key1, seconds, value1);
					return true;
				}
			});
		}catch(Exception e){
			e.printStackTrace();
		}
		return boo;
	}
	

}
